﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using TmU220PrintHelperV2d0.Entity;
using ZhangLogSysV1d0;

namespace TmU220PrintHelperV2d0.Singleton
{
    public class ApiInterfaceInfoSingletonV2d0
    {
        private static ApiInterfaceInfoSingletonV2d0 _instance = null;
        private ApiInterfaceSetV2d0 _apiInterfaceSet = null;
        private static readonly object syslock = new object();
        /// <summary>
        /// 单例模式
        /// </summary>
        public static ApiInterfaceInfoSingletonV2d0 Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (syslock)
                    {
                        if (_instance == null)
                        {
                            _instance = new ApiInterfaceInfoSingletonV2d0();
                        }
                    }
                }
                return _instance;
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        private ApiInterfaceInfoSingletonV2d0()
        {
            _apiInterfaceSet = new ApiInterfaceSetV2d0();
            LoadData();
        }
        /// <summary>
        /// 装载数据
        /// </summary>
        private void LoadData()
        {
            string currDir = System.IO.Directory.GetCurrentDirectory();
            string filename = currDir + @"\parameter\ApiInterfaceInfo.xml";
            _apiInterfaceSet = ReadFromFile(filename);
        }
        private ApiInterfaceSetV2d0 ReadFromFile(string fileName)
        {
            ApiInterfaceSetV2d0 resultDataSet = null;
            try
            {
                if (File.Exists(fileName))
                {
                    StreamReader sR = new StreamReader(fileName);
                    XmlSerializer xmlS = new XmlSerializer(typeof(ApiInterfaceSetV2d0));
                    resultDataSet = (ApiInterfaceSetV2d0)xmlS.Deserialize(sR);
                    sR.Close();
                }
            }
            catch (Exception ex)
            {
                SimpleLoger.Instance.Error(ex);
            }
            return resultDataSet;
        }
        public ApiInterfaceV2d0 GetApiInterfaceFromName(string interfaceName)
        {
            ApiInterfaceV2d0 urlElement = null;
            urlElement = _apiInterfaceSet.ApiInterfaceList.FirstOrDefault(x => x.InterfaceName == interfaceName);
            return urlElement;
        }
    }
}
